package com.ruoyi.flow.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.sign.Base64;
import com.ruoyi.flow.domain.vo.BusBpmnInfoVo;
import org.apache.commons.io.IOUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StreamUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.flow.domain.BusBpmnInfo;
import com.ruoyi.flow.service.IBusBpmnInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 流程定义Controller
 *
 * @author ruoyi
 * @date 2023-10-26
 */
@Validated
@RestController
@RequestMapping("/flow/bpmnInfo")
public class BusBpmnInfoController extends BaseController {
    @Autowired
    private IBusBpmnInfoService busBpmnInfoService;

    /**
     * 查询流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(BusBpmnInfo busBpmnInfo) {
        startPage();
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        return getDataTable(list);
    }

    /**
     * 导出流程定义列表
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:export')")
    @Log(title = "流程定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BusBpmnInfo busBpmnInfo) {
        List<BusBpmnInfo> list = busBpmnInfoService.selectBusBpmnInfoList(busBpmnInfo);
        ExcelUtil<BusBpmnInfo> util = new ExcelUtil<BusBpmnInfo>(BusBpmnInfo.class);
        util.exportExcel(response, list, "流程定义数据");
    }

    /**
     * 获取流程定义详细信息
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(busBpmnInfoService.selectBusBpmnInfoById(id));
    }

    /**
     * 查看流程定义文件
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:query')")
    @GetMapping("/{id}/{type}")
    public void getBpmnFile(@PathVariable("id") Long id, @PathVariable String type, HttpServletResponse httpServletResponse) throws Exception {
        InputStream inputStream = busBpmnInfoService.selectBpmnInfoFile(id, type);
        if (inputStream == null) {
            return;
        }

        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            if ("image".equals(type)) {
                byteArrayOutputStream = new ByteArrayOutputStream();
                StreamUtils.copy(inputStream, byteArrayOutputStream);
                byte[] bytes = byteArrayOutputStream.toByteArray();
                String encode = Base64.encode(bytes);
                String prefix = "";
                if (bytes[0] == (-119) && bytes[1] == 80 && bytes[2] == 78 && bytes[3] == 71) {
                    prefix = "data:image/png;base64,";
                } else {
                    prefix = "data:image/svg+xml;base64,";
                }
                String content = prefix + encode;
                httpServletResponse.getWriter().write(content);
                return;
            }
            IOUtils.copy(inputStream, httpServletResponse.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            inputStream.close();
            if (byteArrayOutputStream != null) {
                byteArrayOutputStream.close();
            }
        }
    }

    /**
     * 新增流程定义部署
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:deploy')")
    @Log(title = "流程定义部署", businessType = BusinessType.INSERT)
    @PostMapping("/deploy")
    public AjaxResult add(@Validated BusBpmnInfoVo busBpmnInfoVo) {
        busBpmnInfoService.deployByBpmnInfo(busBpmnInfoVo);
        return success();
    }

    /**
     * 修改流程定义
     */
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:edit')")
    @Log(title = "流程定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BusBpmnInfo busBpmnInfo) {
        return toAjax(busBpmnInfoService.updateBusBpmnInfo(busBpmnInfo));
    }

    /**
     * 撤销流程定义
     */
    @Transactional(rollbackFor = Exception.class)
    @PreAuthorize("@ss.hasPermi('flow:bpmnInfo:remove')")
    @Log(title = "流程定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {

        return toAjax(busBpmnInfoService.deleteBusBpmnInfoByIds(ids));
    }

}
